package com.osard.test_record;

import android.graphics.Bitmap;
import android.graphics.pdf.PdfDocument;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 图片保存到PDF文件，每张图片一页
 */
public class ImageToPdfUtils {

    public static ImageToPdfUtils createInstance() {
        return new ImageToPdfUtils();
    }

    private int pdfDocumentWidth;
    private int pdfDocumentHeight;

    private List<Bitmap> bitmapList;

    private ImageToPdfUtils() {
        this.pdfDocumentWidth = 1080;
        this.pdfDocumentHeight = 1920;
    }

    /**
     * 设置预期PDF文档的宽高，像素
     * <p>
     * 若图像大小超出此值，则打印图像页面以图像超出的宽或高为准
     */
    public ImageToPdfUtils setPdfDocumentSize(int width, int height) {
        this.pdfDocumentWidth = width;
        this.pdfDocumentHeight = height;
        return this;
    }

    /**
     * 设置打印的图像
     */
    public ImageToPdfUtils setBitmaps(Bitmap... bitmaps) {
        return setBitmaps(Arrays.asList(bitmaps));
    }

    /**
     * 设置打印的图像
     */
    public ImageToPdfUtils setBitmaps(List<Bitmap> bitmaps) {
        this.bitmapList = bitmaps;
        return this;
    }

    /**
     * 保存图像到指定的文件
     *
     * @param fileName 全路径，且不能是分区储存的路径
     * @return 是否保存成功
     */
    public boolean save(String fileName) {
        if (null == bitmapList || bitmapList.isEmpty()) {
            return false;
        }

        PdfDocument document = new PdfDocument();
        for (int i = 0; i < bitmapList.size(); i++) {
            Bitmap bitmap = bitmapList.get(i);
            //得到图片尺寸
            int bitWidth = bitmap.getWidth();
            int bitHeight = bitmap.getHeight();
            //预期尺寸和图片尺寸对比取最大
            int pageWidth = Math.max(bitWidth, pdfDocumentWidth);
            int pageHeight = Math.max(bitHeight, pdfDocumentHeight);
            //绘制每页一张图
            PdfDocument.PageInfo.Builder pb = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, i + 1);
            PdfDocument.PageInfo pageInfo = pb.create();
            PdfDocument.Page page = document.startPage(pageInfo);
            //居中绘制
            page.getCanvas().drawBitmap(bitmap,
                    pageWidth / 2F - bitmap.getWidth() / 2F,
                    pageHeight / 2F - bitmap.getHeight() / 2F,
                    null);
            document.finishPage(page);
        }

        File file = new File(fileName);
        if (!file.exists()) {
            try {
                File parentFile = file.getParentFile();
                if (null != parentFile && !parentFile.exists()) {
                    //noinspection ResultOfMethodCallIgnored
                    parentFile.mkdirs();
                }
                //noinspection ResultOfMethodCallIgnored
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
            document.writeTo(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            document.close();
        }

        return true;
    }

//    public static void bitmapToPdf(Bitmap bitmap) {
//
//        long start = new Date().getTime();
//        PdfDocument document = new PdfDocument();
//        int screenWidth = 1080;
//        int screenHeight = 1920;
//
//        int bitWidth = bitmap.getWidth();
//        int bitHeight = bitmap.getHeight();
//
//        screenWidth = bitWidth;
//        screenHeight = bitHeight;
//
//        int count = 1;
//
//        for (int i = 0; i < count * 10; i++) {
//            PdfDocument.PageInfo.Builder pb = new PdfDocument.PageInfo.Builder(screenWidth, screenHeight, i);
//            PdfDocument.PageInfo pageInfo = pb.create();
//            PdfDocument.Page page = document.startPage(pageInfo);
////            View decorView = list.getChildAt(i % count);
////            decorView.draw(page.getCanvas());
//
//            page.getCanvas().drawBitmap(bitmap,
//                    screenWidth / 2F - bitmap.getWidth() / 2F,
//                    screenHeight / 2F - bitmap.getHeight() / 2F,
//                    null);
//
//            document.finishPage(page);
//        }
//
//        String path = App.APPLICATION.getExternalFilesDir("") + "/test1.pdf";
//        File file = new File(path);
//        if (!file.exists()) {
//            try {
//                file.getParentFile().mkdirs();
//                //noinspection ResultOfMethodCallIgnored
//                file.createNewFile();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        FileOutputStream outputStream;
//        try {
//            outputStream = new FileOutputStream(file);
//            document.writeTo(outputStream);
//            outputStream.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        document.close();
//        long end = new Date().getTime();
//        Log.w("lfddz", "cost time = " + (end - start) / 1000.0f + "s");
//
//    }

}
